$(function () {
    $.idcode.setCode();   //加载生成验证码方法

    var flag=false;
    $(".txtVerification").blur(function(){
        var IsBy = $.idcode.validateCode()  //调用返回值，返回值结果为true或者false
        if(IsBy){
            $(".yzm-error").css("display","none")
            flag=true;
        }else {
            $(".yzm-error").css("display","inline-block")
            flag=false;
        }
    })

    $.validator.addMethod("checkedTel",function (val,input,param) {
        let reg=/^[1]\d{10}$/;
        return reg.test(val);
    },"手机号不合法");

    $("form").validate({
        rules:{
            utel:{
                required:true,
                checkedTel:true,
                remote:{
                    url:"http://127.0.0.1:8080/zhongjiu/server/checkeUser.php",
                    type:"get",
                    data:{
                        "username":function () {
                            return $("input[name=utel]").val();
                        }
                    }
                }
            },
            upwd:{
                required: true,
                rangelength:[6,20],
            },
            upwd1:{
                equalTo:"#upwd"
            },
            agree:"required"
        },
        messages:{
            utel: {
                required:"手机号不能为空",
                remote: "该手机号已被注册",
            },
            upwd: {
                required:"密码不能为空",
                rangelength:"密码长度必须是{0}-{1}",
            },
            upwd1:{
                equalTo: "两次密码不一致"
            },
            agree: "请接受我们的声明"
        },
        submitHandler:function () {
            if (flag){
                $.ajax({
                    url:"http://127.0.0.1:8080/zhongjiu/server/register.php",
                    type:"post",
                    dataType:"json",
                    data:$("form").serialize()
                }).then(function(result){
                    if (result.status==1){
                        confirm("注册成功,是否现在去登录")?window.location="login.html":"";
                    }else {
                        alert("注册失败")
                    }
                })
            }else {
                $(".yzm-error").css("display","inline-block")
            }
            return false;
        }


    })

})